package com.example.demo.str;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 字符串测试
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2023/05/31 14:11
 */
public class StrTest {

    @Test
    public void test() {

        long currentTimeMillis = System.currentTimeMillis();
        String str = "11112aaqqqqeeefjdidasdhasuihduashdhauihIUHASDUhosu" +
                "idhuIHSDHJNFVJKHAUFAHIDFJIOLFASDAJDFIOAJSDFIOAIOPDFIASUDFIJA" +
                "IOJFAKLSDJFASDIOFAIOJSDFIOAJDFIOJASDFHBMNXCBVYGYUAGFBSDF" +
                "3NMBZNBXVYGDFHASDUIFHNXCVNZLXNVZUIXCHVPASDFIAHSVNHHJXCNV" +
                "NM,NVMMNV,.ZNXCVN;SDFUW[WUR- 239R09FKFKLALFASFJSDF9OI09R239-" +
                "0ROSJKDFLK;A;LFASDASDASDASDAWQWE123FGFGSFGSDGFkjsdfkjakd" +
                "sjfkljdfs" +
                "129389023849085sdaASD545645545aYUINBVCFCCBBNMM&" +
                "*（（…………%￥￥￥#）（*……&%￥￥##@！@#￥%……&**（）456454654444444" +
                "4444444444444444444444444444444444444444431231321321232132123" +
                "132" +
                "123121212121213";


        String subStr = "12a";

        String[] strArr = str.split("");
        String[] subStrArr = subStr.split("");

        List<String> result = new ArrayList<>();
        boolean b = false;
        int i = 0;
        int j = 0;
        while (i < strArr.length) {
            if (strArr[i].equals(subStrArr[j])) {
                if (!b) {
                    b = true;
                    result.add(i + "");
                }
                i++;
                j++;
                if (j >= subStrArr.length) {
                    b = false;
                    j = 0;
                }
            } else {
                if (b) {
                    b = false;
                    result.remove(result.size() - 1);
                }
                if (j != 0) {
                    j = 0;
                } else {
                    i++;
                }
            }
        }
        System.out.println(result);
        for (String s : result) {
            Integer integer = Integer.valueOf(s);
            String substring = str.substring(integer, integer + subStr.length());
            System.out.println(substring);
        }

        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println(currentTimeMillis1 - currentTimeMillis);
    }


    public static void main(String[] args) {
        String str1 = "BBC ABCDAB ABCDABCDABDE";
        String str2 = "ABCDABD";
        //String str2 = "BBC";
        int[] next = kmpNext("ABCDABD"); //[0, 1, 2, 0]
        System.out.println("next=" + Arrays.toString(next));
        int index = kmpSearch(str1, str2, next);
        System.out.println("index=" + index); // 15 了
    }

    /**
     * KMP算法
     *
     * @param str1 源字符串
     * @param str2 子串
     * @param next 部分匹配表, 是子串对应的部分匹配表
     * @return 如果是-1 就是没有匹配到，否则返回第一个匹配的位置
     */
    public static int kmpSearch(String str1, String str2, int[] next) {
        //遍历
        for (int i = 0, j = 0; i < str1.length(); i++) {
            //需要处理 str1.charAt(i) != str2.charAt(j), 去调整 j 的大小
            //KMP 算法核心点, 可以验证...
            while (j > 0 && str1.charAt(i) != str2.charAt(j)) {
                j = next[j - 1];
            }

            if (str1.charAt(i) == str2.charAt(j)) {
                j++;
            }
            //找到了 // j = 3 i
            if (j == str2.length()) {
                return i - j + 1;
            }
        }
        return -1;
    }

    /**
     * 获取到一个字符串(子串) 的部分匹配值表
     *
     * @param dest
     * @return
     */
    public static int[] kmpNext(String dest) {
        //创建一个 next 数组保存部分匹配值
        int[] next = new int[dest.length()];
        //如果字符串是长度为 1 部分匹配值就是 0
        next[0] = 0;
        for (int i = 1, j = 0; i < dest.length(); i++) {
            //当 dest.charAt(i) != dest.charAt(j) ，我们需要从 next[j-1]获取新的 j
            //直到我们发现 有 dest.charAt(i) == dest.charAt(j)成立才退出
            //这时 kmp 算法的核心点
            while (j > 0 && dest.charAt(i) != dest.charAt(j)) {
                j = next[j - 1];
            }
            //当 dest.charAt(i) == dest.charAt(j) 满足时，部分匹配值就是+1
            if (dest.charAt(i) == dest.charAt(j)) {
                j++;
            }
            next[i] = j;
        }
        return next;
    }
}
